
function ztreeSelect(opt){



	//全局变量
	var wrap = '<div class="jBoxDeptSelect"><ul id="zTreeDeptSelect" class="ztree">loading...</ul></div>';
	var zTree = null; //zTree对象
	var zNodes = [];  //数据

	//默认配置变量
	var def = {
		multi : true ,// 多选true 单选false
		url_notes : "/admin/role/zNodes.html",//获取url 
		ids:"1",
		ok:function(nodes){console.log('ok:function : ' , nodes );},
		type:null , //  1. 只有isLastNode有选择框


	};
	//使用的配置变量
	var o = Object.assign({}, def , opt );
console.log(o);
	//函数主题
	var m = {}

	//ztree 配置
	var setting = {
		check: {
				enable: true,
				chkStyle: o.multi ? "checkbox" : "radio" ,
				radioType: "all"

			},

		view: {
			dblClickExpand: false,  //双击展开
			showLine: true,        //是否显示 连接线
			selectedMulti: o.multi,   //设置是否允许同时选中多个节点。默认值: true
			//showIcon:true , //是否显示图标
		},
		data: {
			key:{
				name: "name",
			},
			simpleData: {
				enable:true, 
				idKey: "id",
				pIdKey: "pid",
				rootPId: ""
			},
			

		},
		callback: {
			beforeClick:beforeClick, 
			onClick:onClick,
	        onExpand: onExpand,  // 展开
	        onCollapse: onCollapse,  // 折叠  
	        onCheck: zTreeOnCheck,

		}
	};


	// zTree展开 回调函数
    function onExpand(event, treeId, treeNode){  
        var cookie = $.cookie("z_tree");  
          
        var z_tree = null;  
        if(cookie){  
            z_tree = JSON.parse(cookie);  
        }  
          
        if(!z_tree){  
            z_tree = new Array();  
        }  
        if(jQuery.inArray(treeNode.id, z_tree)<0){  
            z_tree.push(treeNode.id); //追加 
        }  
          
          
        $.cookie("z_tree", JSON.stringify(z_tree))  
    }  


    // zTree折叠   回调函数
    function onCollapse(event, treeId, treeNode){  
        var cookie = $.cookie("z_tree");  
          
        var z_tree = null;  
        if(cookie){  
            z_tree = JSON.parse(cookie);  
        }  
          
        if(!z_tree){  
            z_tree = new Array();  
        }  
        var index = jQuery.inArray(treeNode.id, z_tree);  
          
        z_tree.splice(index, 1); //删除  
          
        $.cookie("z_tree", JSON.stringify(z_tree))  
    }

    //zTree
    function beforeClick(treeId, treeNode, clickFlag) {
		// console.log('beforeClick:' ,treeNode , clickFlag);

		zTree.checkNode( treeNode , true, true); //设置被选中

		zTree_setCheckNodeSelected();
		// return (treeNode.checked );
		return false;
		
	}
	function zTreeOnCheck(event, treeId, treeNode) {
	    zTree_setCheckNodeSelected();
	};

	//设置 check node  select
	function zTree_setCheckNodeSelected()
	{

		console.log('zTree_setCheckNodeSelected : 设置选中的 文字 选中');
		var nodes = getCheckedNodes();//获取选中的节点 

		for(var i = 0 ,len = nodes.length ; i< len ; i++){
			zTree.selectNode(nodes[i], !!i  ); //第二参数 i  , 第一次false 之后都是true
 		} 
	}

	//获取选中节点  zTree.getCheckedNodes(true); 获取的包含 半选中  需要过滤
	function getCheckedNodes(){
		var nodes = zTree.getCheckedNodes(true);//获取选中的节点  全勾 半勾
		var node2 = [];
		for(var i = 0 ,len = nodes.length ; i< len ; i++){
			//过滤掉 半勾的
			if( ! nodes[i].getCheckStatus().half ){ node2.push(nodes[i]); }
			
 		} 
 		return node2;

	}

	function onClick(event, treeId, treeNode, clickFlag){
	
	}

	//设置选中  by id
	function zTree_setCheckNodeById( ids = null){
		if(!ids){return false;}

		if( typeof(ids) == 'string' ){
			ids =  trim(ids);
			ids =  ids.split(',');
		};
		

		console.log('lrxin'  , ids );
		for(let i=0 , len = ids.length ; i< len ; i++){
			var id = ids[i];
			var treeNode  = zTree.getNodeByParam("id", id, null);
			zTree.checkNode( treeNode , true, true); //设置被选中
			zTree_setCheckNodeSelected()
		}
		

		// zTree.checkNode( treeNode , true, true); //设置被选中
		// zTree_setCheckNodeSelected()
	}
	//
	function zTree_init(){
		


		//jBox.tip('loading...' , 'loading');
	    $.ajax({
	        type:'post',
	        url:o.url_notes,
	        data: {},
	        dataType:'json',
	        success:function(d){

	        		//jBox.closeTip();
	        		zNodes = d;
	        		zTree_create();

	        		zTree_setCheckNodeById( o.ids );

	        },
	        error:function(a,b,c){
	            jBox.tip('ajax error：' + b );
	        }
	    });

	}


	//
	function zTree_create(){
			
		zTree = $.fn.zTree.init($('#zTreeDeptSelect'), setting, zNodes);
		//根据cookie 展开 ztree
		var cookie = $.cookie("z_tree");  
	    if(cookie){  
	        z_tree = JSON.parse(cookie);  
	        for(var i=0; i< z_tree.length; i++){  
		        var node = zTree.getNodeByParam('id', z_tree[i])  
		        zTree.expandNode(node, true)  
	        }  
	    }  
	    console.log(o.type);

	    if(o.type == 1 ){ zTree_parent_nocheck(); }
	}

	//展开折叠切换
	var flag_expand = false;
	function expandAll(obj){
		if(! $(obj).length ) return false ;
		var z_tree = [];

		if( flag_expand ){
			$(obj).text(' 展开');
			//z_tree = [];

		}else{
			$(obj).text(' 折叠');
			for(var i = 0 , len = zNodes.length ; i < len  ;  i++  ){
				z_tree.push( zNodes[i].id );
			}
		}

		$.cookie("z_tree", JSON.stringify(z_tree));
		zTree.expandAll(flag_expand = !flag_expand);
		// var node = zTree.getNodeByParam("id", 5);
		// zTree.selectNode(node, true, true);
		// zTree.checkNode(node, true, true);

	}



	 // 窗口加载完成后执行的函数，参数h表示窗口内容的jQuery对象 
	function jboxLoaded(h){
		h.html(wrap);
		zTree_init();
		
	}
	 // 点击窗口按钮后的回调函数，返回true时表示关闭窗口，参数有三个，v表示所点的按钮的返回值，h表示窗口内容的jQuery对象，f表示窗口内容里的form表单键值 
    function jboxSubmit(v, h, f) { 
    	var $ids = [];
    	var $names = [];
    	var nodes = getCheckedNodes(true);//获取选中的节点 

    	
    	if( typeof(o.ok) == 'function' ){ o.ok(nodes) };
    	

  //   	for(var i = 0 ,len = nodes.length ; i< len ; i++){
		// 	$ids.push( nodes[i].id );
		// 	$names.push( nodes[i].name );
		// } 

		// $ids   = $ids.join(',');
		// $names = $names.join(',');

		// console.log( $ids , $names );

		// $selector_name.val($names);
		// $selector_id.val($ids);

	

    	return true; 
	}
     // 窗口关闭后执行的函数 
    function jboxClosed() { } 

	jBox( 'loading', 
		{  title:'部门选择器' ,  
			bottomText: '' , 
			loaded:jboxLoaded , 
			submit:jboxSubmit ,
			closed:jboxClosed 
		}
	);


	//其他
	function trim(str){return str.replace(/(^\s*)|(\s*$)/g,"");}




	//
	function zTree_parent_nocheck(){
		console.log('zTree_parent_nocheck:');
		var nodes = zTree.getNodesByFilter(function(node){
			return node.isParent ;
		});
		for (var i = 0; i < nodes.length; i++) {
			var node = nodes[i];
			node.nocheck = true; //表示显示checkbox
			zTree.updateNode(node);
		}
	}

}